//
//  TransferOfRepairOrderView.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/29.
//

import UIKit

extension PTSheetHandle {
  /// 维修转单
  func sheet(repairTrans: RepairInformationModel?, handle: @escaping Handle) {
    let sheet = R.nib.transferOfRepairOrderView(owner: nil)!
    sheet.titleLabel.text = "维修转单"
    sheet.model = repairTrans
    self.sheet(PTSheetHandle.Content(sheet: sheet), handle: handle)
  }

  /// 分配师傅
  func sheet(masterSelect: RepairInformationModel?, handle: @escaping Handle) {
    let sheet = R.nib.transferOfRepairOrderView(owner: nil)!
    sheet.titleLabel.text = "分配维修师傅"
    sheet.model = masterSelect
    self.sheet(PTSheetHandle.Content(sheet: sheet), handle: handle)
  }
}

import CollectionKit

/// 维修转单
class TransferOfRepairOrderView: UIView {

  private var collectionView = CollectionView()

  @IBOutlet weak var titleLabel: UILabel!
  @IBOutlet weak var ensureButton: UIButton!

  @IBOutlet weak var contentView: UIView!

  private var completeHandle: PTSheetHandle.Handle?
  var model: RepairInformationModel?

  override func awakeFromNib() {
    super.awakeFromNib()

    let selectDataSource = ArrayDataSource<String>(data:[
      "冉龙鑫", "乌启豪", "敖溪", "丙胄", "厉贾", "飞流"
    ])

    let selectViewSource = ClosureViewSource<String, SelectItemContentView> { data, index in
      return R.nib.selectItemContentView(owner: nil)!
    } viewUpdater: { item, data, index in
      item.titleLabel.text = data
    }

    let selectSizeSource = { (index: Int, data: String, size: CGSize) in
        return CGSize(
          width: (size.width - 12) / 2,
          height: 34
        )
    }

    let selectProvider = BasicProvider<String, SelectItemContentView>(
      dataSource: selectDataSource,
      viewSource: selectViewSource,
      sizeSource: selectSizeSource,
      layout: FlowLayout(spacing: 12, justifyContent: .spaceEvenly)
    )

    selectProvider.tapHandler = { (context) in

    }

    collectionView.provider = ComposedProvider(
        sections: [selectProvider]
    )

    contentView.addSubview(collectionView)
    collectionView.snp.makeConstraints { make in
      make.edges.equalToSuperview()
    }
  }

  @IBAction func tappedCancel(_ sender: Any) {
    completeHandle?(.cancel)
  }

  @IBAction func tappedEnsure(_ sender: Any) {
    completeHandle?(.ensure)
  }
}

extension TransferOfRepairOrderView: PTSheetConfigurable {

  var handle: PTSheetHandle.Handle? {
      get {
          return completeHandle
      }
      set {
          completeHandle = newValue
      }
  }

  func shouldUpdateLayoutConstraints() {

    self.snp.remakeConstraints { (make) in
      make.height.equalTo(307)
      make.center.equalToSuperview()
      make.width.lessThanOrEqualTo(320)
      make.left.equalToSuperview().offset(47).priority(.high)
      make.right.equalToSuperview().offset(-47).priority(.high)
    }
  }
}
